package day_2022_9_to_12.Day;

import java.util.HashMap;
import java.util.Map;

/**
 * @author haomin
 * @date 2022/10/01 22:29
 **/
public class Day30_2_Num6212_EqualFrequency {
    class Solution {
        public boolean equalFrequency(String word) {

            Map<Character, Integer> map = new HashMap<>();
            for(char c : word.toCharArray()){
                map.put(c, map.getOrDefault(c, 0) + 1);
            }
            Map<Integer, Integer> res = new HashMap<>();
            for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                int val = entry.getValue();
                res.put(val, map.getOrDefault(val, 0) + 1);
            }
            if(res.size() > 2) return false;
            if(res.size() == 1) {
                for (Map.Entry<Integer, Integer> entry : res.entrySet()) {
                    if(entry.getKey() == 1) return true;
                    else return false;
                }
            }
            int a = 0, b = 0;
            for (Map.Entry<Integer, Integer> entry : res.entrySet()) {
                if(a == 0) a = entry.getKey();
                else b = entry.getKey();
            }
            return Math.abs(a-b) == 1;
        }
    }
    class Solution3 {
        public boolean equalFrequency(String word) {
            for(int i = 0; i < word.length()-1; ++i){
                String str = word.substring(0, i) + word.substring(i+1);
                if(find(str)) return true;
            }
            return false;
        }
        private boolean find(String str){
            Map<Character, Integer> map = new HashMap<>();
            for(char c : str.toCharArray()){
                map.put(c, map.getOrDefault(c, 0) + 1);
            }
            int i = 0;
            for (Map.Entry<Character, Integer> e : map.entrySet()) {
                if(i == 0) i = e.getValue();
                else if(i != e.getValue()) return false;
            }
            return true;
        }
    }
}